package club.banyuan.hrms.dao.impl;

import club.banyuan.hrms.dao.PositionDao;
import club.banyuan.hrms.domain.Position;
import club.banyuan.hrms.util.DbUtil;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class PositionDbDaoImpl implements PositionDao {
    @Override
    public List<Position> getPositions() {
        List<Position> positions = new ArrayList<>();
        String sql = "SELECT * FROM t_position";


        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()
        ) {
            while (resultSet.next()) {
                Position position = new Position();
                position.setId(resultSet.getInt("id"));
                position.setName(resultSet.getString("name"));
                position.setDescription(resultSet.getString("description"));

                positions.add(position);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return positions;
    }

    @Override
    public void updatePosition(Position position) {
        String sql = "UPDATE t_position SET name=?,description=? WHERE id=?";

        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {
            preparedStatement.setString(1, position.getName());
            preparedStatement.setString(2, position.getDescription());
            preparedStatement.setInt(3, position.getId());

            int rows = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addPosition(Position position) {
        String sql = "INSERT INTO t_position(name,description) VALUES(?,?)";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {
            preparedStatement.setString(1, position.getName());
            preparedStatement.setString(2, position.getDescription());

            int rows = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    @Override
    public Position getPositionById(Integer id) {
        String sql = "SELECT * FROM t_position WHERE id=?";

        ResultSet resultSet = null;
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            int rows = preparedStatement.executeUpdate();

            while (resultSet.next()) {
                String name = resultSet.getString("name");
                String description = resultSet.getString("description");

                Position position = new Position();
                position.setId(id);
                position.setName(name);
                position.setDescription(description);

                return position;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    public void deletePosition(Integer[] ids) {
        String sql = "DELETE FROM t_position WHERE id IN (?";
        for (int i = 1; i < ids.length; i++) {
            sql += ",?";
        }
        sql += ")";

        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {
            for (int i = 0; i < ids.length; i++) {
                preparedStatement.setInt(i + 1, ids[i]);
            }
            Integer rows = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
